package com.markbuikema.juliana32.adapter;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;
import com.markbuikema.juliana32.R;
import com.markbuikema.juliana32.model.nieuws.FacebookNieuwsItem;
import com.markbuikema.juliana32.util.Util;

public class PhotoPagerAdapter extends PagerAdapter implements Observer {

	private FacebookNieuwsItem item;
	private View view;

	public PhotoPagerAdapter(Context context, FacebookNieuwsItem item, View view) {
		super();
		item.addObserver(this);
		this.item = item;
		this.view = view;
	}

	@Override
	public Object instantiateItem(final ViewGroup container, final int position) {

		final ImageView image = (ImageView) LayoutInflater.from(container.getContext()).inflate(R.layout.card_photo_item, null);

		UrlImageViewHelper.setUrlDrawable(image, Util.PHOTO_URL_PREFIX + item.getPhotoId(position) + Util.PHOTO_URL_SUFFIX);
		image.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				view.performClick();
			}
		});
		((ViewPager) container).addView(image);

		return image;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public int getCount() {
		return item.getPhotoCount();
	}

	@Override
	public boolean isViewFromObject(View view, Object key) {
		return view == key;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		notifyDataSetChanged();
	}

}